package au.edu.unsw.infs3634.gamifiedlearning;

import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import org.w3c.dom.Text;

import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.API.Country;
import au.edu.unsw.infs3634.gamifiedlearning.API.CountryService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CountryInfo extends AppCompatActivity {
    public String name;
    public static final String countryName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country_info);

        // Back button
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //Get Intent and set display
        Intent newIntent = getIntent();
        name = newIntent.getStringExtra(countryName);
        setTitle(name);
        //retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://restcountries.com/v3.1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CountryService service = retrofit.create(CountryService.class);

        Call<List<Country>> responseCall = service.getName(name);
        Log.d("Response Call", responseCall.toString());
        responseCall.enqueue(new Callback<List<Country>>() {
            @Override
            public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {
                List<Country> countryInfo = response.body();
                Log.d("Country Name", name);
                TextView countryName  = findViewById(R.id.tvCountryInfoName);

                TextView population = findViewById(R.id.tvCountryInfoPopulation);
                TextView capital = findViewById(R.id.tvCountryInfoCapital);
                TextView timeZone = findViewById(R.id.tvCountryInfoTimeZone);
                TextView continents = findViewById(R.id.tvCountryInfoContinents);
                TextView fullName = findViewById(R.id.tvCountryInfoFullName);
                TextView cca3 = findViewById(R.id.tvCountryInfoCCA3);
                TextView phone = findViewById(R.id.tvCountryInfoPhone);
                TextView region = findViewById(R.id.tvCountryInfoRegion);


                countryName.setText(name);
                population.setText("Population: "+countryInfo.get(0).getPopulation().toString());
                fullName.setText("Official name: "+countryInfo.get(0).getName().getOfficial());
                if (countryInfo.get(0).getCapital()==null){
                    capital.setText("Captial: N/A");
                }else {
                    capital.setText("Captial: "+countryInfo.get(0).getCapital().toString().replaceAll("[\\[\\](){}]",""));
                }
                timeZone.setText("TimeZone: "+countryInfo.get(0).getTimezones().toString().replaceAll("[\\[\\](){}]",""));
                continents.setText("Continent: "+countryInfo.get(0).getContinents().toString().replaceAll("[\\[\\](){}]",""));
                cca3.setText("Country code: "+countryInfo.get(0).getCca3());
                phone.setText("IDD number: "+countryInfo.get(0).getIdd().getRoot()+countryInfo.get(0).getIdd().getSuffixes().toString().replaceAll("[\\[\\](){}]",""));
                region.setText("Region: "+countryInfo.get(0).getSubregion());
                Glide.with(findViewById(R.id.ivFlag))
                        .load(countryInfo.get(0).getFlags().getPng())
                        .override(400, 500)
                        .into((ImageView) findViewById(R.id.ivFlag));
            }

            @Override
            public void onFailure(Call<List<Country>> call, Throwable t) {
                Log.d("No", String.valueOf(t));
            }
        });


        //Learn more button
        Button btnWebsite = findViewById(R.id.btnWebsite);
        btnWebsite.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View detailView) {
                Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/search?q=" + name));
                startActivity(webIntent);
            }
        });
    }
    //Back button
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}
